package com.zfc.life.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zfc.life.domain.Resource;
import com.zfc.life.domain.vo.ResourceVo;
import com.zfc.life.mapper.ResourceMapper;
import com.zfc.life.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zfc
 * @since 2024-05-23
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public Integer addResource(Resource resource) {
        return resourceMapper.addResource(resource);
    }

    @Override
    public PageInfo<Resource> listResource(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Resource> page = resourceMapper.listResource();
        PageInfo<Resource> info = new PageInfo<>(page);
        info.setSize(page.size());
        return info;
    }

    @Override
    public Resource querySubjectById(Long id) {

        return resourceMapper.querySubjectById(id);
    }

    @Override
    public Integer modifySubject(Resource resource) {
        return resourceMapper.modifySubject(resource);
    }

    @Override
    public void deleteSubject(Long id) {
        resourceMapper.deleteSubject(id);
    }

    @Override
    public Integer addSubjectDetail(Resource resource) {

        return resourceMapper.addSubjectDetail(resource);
    }

    @Override
    public List<ResourceVo> frontendResourceList() {
        return resourceMapper.frontendResourceList();
    }

    @Override
    public List<String> selectResourceByParent(Long parent) {
        List<Resource> resourceList = resourceMapper.selectResourceByParent(parent);

        if (resourceList.size() > 0){
            String resourcePath = "";

            //1、遍历所有资源地址，拼接成字符串
            for (Resource resource : resourceList) {
                resourcePath += resource.getResourcePath();
            }
            //2、拆分字符串，根据;
            String[] split = resourcePath.split(";");

            List<String> resourcePathList = new ArrayList<>();

            //3、遍历拆分的字符串，添加到集合中
            for (String resource : split) {
                resourcePathList.add(resource);
            }
            return resourcePathList;
        }


        return null;
    }
}
